package bll

import (
	"context"

	"manager/event"
	"manager/model"
	"manager/model/entity"
	"manager/store"
	"manager/store/postgres"
	"time"
)

type deviceUpgradeRecode struct {
	iDeviceUpgradeRecode store.IDeviceUpgradeRecode
}

var DeviceUpgradeRecode = &deviceUpgradeRecode{
	iDeviceUpgradeRecode: postgres.DeviceUpgradeRecode,
}

func (a *deviceUpgradeRecode) init() func() {
	return func() {}
}

func (a *deviceUpgradeRecode) onEvent(*event.Data) {}

// Create 创建
func (a *deviceUpgradeRecode) Create(ctx context.Context, in *model.DeviceUpgradeRecodeCreateRequest) error {
	var (
		err error
	)

	// 构建创建现场数据
	c := buildDeviceUpgradeRecode(in)
	_, err = a.iDeviceUpgradeRecode.Create(ctx, c)
	return err
}

// Update 更新
func (a *deviceUpgradeRecode) Update(ctx context.Context, in *model.DeviceUpgradeRecodeUpdateRequest) error {
	var (
		dict = make(map[string]interface{})
	)

	updateAt := time.Now().Unix()
	in.UpdatedAt = &updateAt

	if in.FirmwareId != nil {
		dict["firmware_id"] = in.FirmwareId
	}

	if in.FirmwaresName != nil {
		dict["firmwares_name"] = in.FirmwaresName
	}

	if in.Version != nil {
		dict["version"] = in.Version
	}

	if in.TacticsId != nil {
		dict["tactics_id"] = in.TacticsId
	}

	if in.SnCode != nil {
		dict["sn_code"] = in.SnCode
	}

	if in.Desc != nil {
		dict["desc"] = in.Desc
	}

	if in.Status != nil {
		dict["status"] = in.Status
	}

	if in.UpdatedAt != nil {
		dict["updated_at"] = in.UpdatedAt
	}

	// do other update here
	return a.iDeviceUpgradeRecode.Update(ctx, in.Id, dict)
}

// Delete 删除
func (a *deviceUpgradeRecode) Delete(ctx context.Context, in *model.DeviceUpgradeRecodeDeleteRequest) error {
	return a.iDeviceUpgradeRecode.Delete(ctx, in.Id)
}

// List 列表查询
func (a *deviceUpgradeRecode) List(ctx context.Context, in *model.DeviceUpgradeRecodeListRequest) (*model.DeviceUpgradeRecodeListResponse, error) {
	var (
		err   error
		total int
		list  []*entity.DeviceUpgradeRecode
		out   = &model.DeviceUpgradeRecodeListResponse{}
	)

	if total, list, err = a.iDeviceUpgradeRecode.List(ctx, in); err != nil {
		return nil, err
	}

	out.Total = total
	out.List = model.DeviceUpgradeRecodesEntityToDto(list)

	return out, nil
}

// Find 列表查询
func (a *deviceUpgradeRecode) Find(ctx context.Context, in *model.DeviceUpgradeRecodeInfoRequest) (*model.DeviceUpgradeRecodeInfo, error) {
	var (
		err  error
		data *entity.DeviceUpgradeRecode
		out  = &model.DeviceUpgradeRecodeInfo{}
	)

	if data, err = a.iDeviceUpgradeRecode.Find(ctx, in); err != nil {
		return nil, err
	}

	out = model.DeviceUpgradeRecodeEntityToDto(data)
	return out, nil
}

// buildDeviceUpgradeRecode 构建创建数据现场
func buildDeviceUpgradeRecode(in *model.DeviceUpgradeRecodeCreateRequest) *entity.DeviceUpgradeRecode {
	// todo: check the entity is required
	return &entity.DeviceUpgradeRecode{

		FirmwareId: in.FirmwareId,

		FirmwaresName: in.FirmwaresName,

		Version: in.Version,

		TacticsId: in.TacticsId,

		SnCode: in.SnCode,

		Desc: in.Desc,

		Status: in.Status,

		CreatedAt: time.Now().Unix(),

		UpdatedAt: time.Now().Unix(),
	}
}
